package cn.shujuhai.cms.controller.recipient;


import cn.shujuhai.cms.domain.Recipient;
import cn.shujuhai.cms.service.RecipientService;
import cn.shujuhai.common.aop.SysLog;
import cn.shujuhai.common.domain.PageRequest;
import cn.shujuhai.common.pluging.data.ResultVo;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author: NiuYaHu
 * @Date: 2024/12/25 15:01
 * @Description:
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("cms/recipient")
public class RecipientController {
    final RecipientService recipientService;

    /**
     * 查询收件人列表
     *
     * @param email       收件人邮箱
     * @param pageRequest
     * @return 查询的收件人列表
     */
    @SysLog(value = "收件人列表查询", operationType = "查询", module = "收件人模块")
    @GetMapping("/list")
    public ResultVo<PageInfo<Recipient>> getRecipientList
    (@Validated String email, @Valid PageRequest pageRequest, Long groupId) {
        return this.recipientService.getRecipientPageInfo(email, pageRequest.getPageNum(), pageRequest.getPageSize(), groupId);
    }

    /**
     * 新增
     *
     * @param recipient 新增收件人对象
     * @return 新增收件人对象
     */
    @SysLog(value = "新增收件人", operationType = "新增", module = "收件人模块")
    @PostMapping("/add")
    public ResultVo<?> save(@RequestBody @Validated Recipient recipient) {
        return this.recipientService.add(recipient);
    }

    /**
     * 收件人编辑
     *
     * @param Recipient 编辑收件人对象
     * @return 更新收件人对象
     */
    @SysLog(value = "收件人编辑", operationType = "编辑", module = "收件人模块")
    @PostMapping("/update")
    public ResultVo<Recipient> update(@RequestBody @Validated Recipient Recipient) {
        return this.recipientService.edit(Recipient);
    }

    /**
     * 删除收件人
     *
     * @param id 收件人Id
     * @return 删除响应结果
     */
    @SysLog(value = "删除收件人", operationType = "删除", module = "收件人模块")
    @PostMapping("/delete/{id}")
    public ResultVo<Recipient> delete(@PathVariable Integer id) {
        return this.recipientService.removeId(id);
    }

    /**
     * 收件人批量导入
     *
     * @param file 收件人excel表格
     * @return 导入成功或失败的结果及响应信息
     */
    @PostMapping("/import")
    public ResultVo<?> importExcel(@RequestParam("file") MultipartFile file, Integer id) throws IOException {
        return this.recipientService.importRecipients(file, id);
    }

    @GetMapping("/download")
    public void downloadTemplate(HttpServletResponse response) {
        // 调用服务层方法下载模板
        this.recipientService.downloadTemplate(response);
    }

    /**
     * 根据分组Id查询收件人列表
     *
     * @param groupId 分组Id
     * @return 收件人列表
     */
    @GetMapping("/recipientListByGroupId")
    public ResultVo<?> recipientListByGroupId(Long groupId) {
        return this.recipientService.recipientListByGroupId(groupId);
    }
}
